//
// CS1122 October 2008: Lab4
// Louis Natanson
// to work this needs the cpps to go with the following headers
//

#include "stdafx.h"

#include <stdlib.h>
#include "AfterBurnerClone.h"
#include "../d3d/d3dUtility.h"
#include "../Framework/Mouse.h"

//
// Globals
//
IDirect3DDevice9 *Device = NULL;
MyGame::AfterBurnerClone game;

void Cleanup();
bool Display(float);
bool Setup();

void Cleanup() {
}

bool Setup() {
    return true;
}

bool Display(float timeDelta) {
    game.Update(timeDelta);
    if (Device) {
        game.BeginDraw();
        game.Draw(timeDelta);
        game.EndDraw();
    }

    return true;
}

//
// WinMain
//
int WINAPI WinMain(
    HINSTANCE hinstance,
    HINSTANCE prevInstance,
    PSTR cmdLine,
    int showCmd) {
    bool NotfullScreen;

    NotfullScreen = !(MessageBox(NULL, "Run in fullscreen Mode?", "Display Mode", MB_YESNO)==IDYES);

    int width = NotfullScreen ? 800 : INT_MAX;
    int height = NotfullScreen ? 600 : INT_MAX;
    if (!d3d::InitD3D(hinstance, width, height, NotfullScreen, D3DDEVTYPE_HAL, &Device)) {
        MessageBox(0, "InitD3D() - FAILED", 0, 0);
        return 0;
    }

    D3DCAPS9 caps;
    Device->GetDeviceCaps(&caps);

    if (caps.VertexShaderVersion < 3 || caps.PixelShaderVersion < 3) {
        MessageBox(NULL, "This game requires shader version 3 or higher", "Graphic card fail", MB_OK);
        return 0;
    }

    if (!Setup()) {
        MessageBox(0, "Setup() - FAILED", 0, 0);
        return 0;
    }

    RAWINPUTDEVICE Rid[2];

    // Keyboard
    Rid[0].usUsagePage = 1;
    Rid[0].usUsage = 6;
    Rid[0].dwFlags = 0;
    Rid[0].hwndTarget=NULL;

    // Mouse
    Rid[1].usUsagePage = 1;
    Rid[1].usUsage = 2;
    Rid[1].dwFlags = 0;
    Rid[1].hwndTarget=NULL;

    RegisterRawInputDevices(Rid, 2, sizeof(RAWINPUTDEVICE));

    game.Run(Device);

    d3d::EnterMsgLoop(Display);
    
    Cleanup();

    Device->Release();

#ifdef PROFILE_MEMORY
    _CrtDumpMemoryLeaks();
#endif
    return 0;
}


//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch ( msg ) {
    case WM_DESTROY:
        ::PostQuitMessage(0);
        break;
    case WM_KEYDOWN:
        if ( wParam == VK_ESCAPE )
            ::DestroyWindow(hwnd);
        break;
    case WM_INPUT:
        // Determine how big the buffer should be
        UINT bufferSize;
        GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &bufferSize, sizeof (RAWINPUTHEADER));

        // Create a buffer of the correct size - but see note below
        BYTE *buffer=new BYTE[bufferSize];

        // Call the function again, this time with the buffer to get the data
        GetRawInputData((HRAWINPUT)lParam, RID_INPUT, (LPVOID)buffer, &bufferSize, sizeof (RAWINPUTHEADER));

        RAWINPUT *raw = (RAWINPUT*) buffer;
        if (raw->header.dwType == RIM_TYPEMOUSE) {
            Framework::Mouse::Update(raw->data.mouse);
        }

        if (raw->header.dwType == RIM_TYPEKEYBOARD) {
        }

        delete [] buffer;
        break;
    }
    return ::DefWindowProc(hwnd, msg, wParam, lParam);
}
